package com.ruoyi.common.utils.jm.ofd;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;


import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;

public class AKSealForAgreement {

	private static  BigInteger toBigInteger(byte[] v)
	{
		BigInteger bigInt = new BigInteger(1,v);
		return bigInt;
	}

	public static byte[] CreateTbs(byte[] bSeal,String sSignTime,byte[] bDocHash,String sDocProperty,byte[] bSignCert)
	{
		ASN1EncodableVector v = new ASN1EncodableVector();
		//数据版本
		v.add(new ASN1Integer(2));
		//印章
		try {
			ASN1Sequence seal = (ASN1Sequence)ASN1Sequence.fromByteArray(bSeal);
			v.add(seal);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		//签章时间
		v.add(new DERBitString(sSignTime.getBytes()));
		//文档HASH
		v.add(new DERBitString(bDocHash));
		//文档属性值
		v.add(new DERIA5String(sDocProperty));
		//签名证书
		v.add(new DEROctetString(bSignCert));
		//签名算法
		v.add(new ASN1ObjectIdentifier("1.2.156.10197.1.501"));

		DERSequence der = new DERSequence(v);
		try {
			return der.getEncoded();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static ASN1EncodableVector CreateTbsEx(byte[] bSeal,String sSignTime,byte[] bDocHash,String sDocProperty,byte[] bSignCert)
	{
		ASN1EncodableVector v = new ASN1EncodableVector();
		v.add(new ASN1Integer(2));
		try {
			ASN1Sequence seal = (ASN1Sequence)ASN1Sequence.fromByteArray(bSeal);
			v.add(seal);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		//签章时间
		v.add(new DERBitString(sSignTime.getBytes()));
		//文档HASH
		v.add(new DERBitString(bDocHash));
		//文档属性值
		v.add(new DERIA5String(sDocProperty));
		//签名证书
		v.add(new DEROctetString(bSignCert));
		//签名算法
		v.add(new ASN1ObjectIdentifier("1.2.156.10197.1.501"));
		return v;
	}

	public static byte[] JMJSign(byte[] bCert,byte[] bSeal,String sSignTime,byte[] bDocHash,String sDocProperty,int iPrvKeyIndex){
//		//计算待签名数据
//		byte[] bForSign = CreateTbs(bSeal,sSignTime,bDocHash,sDocProperty,bCert);
//		if(bForSign == null)
//			return null;
//		//计算待签名数据的HASh
//		byte[] bHash = SM2CryptoForAgreement.GetHashForSign(bCert, bForSign);
//
//		//调用加密机签名
//    	String sConfFilePath = System.getenv("YUNHSMSDK_CONF");
//		if (sConfFilePath == null) {
//		    System.out.println("getenv(\"YUNHSMSDK_CONF\") return null, please set this env variable");
//		    return null;
//		}
//		try {
//			PropertiesUtil.init();
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String sPassword = PropertiesUtil.get("JMJPWD");
//		int ret;
//		long[] dev = {0};
//		long[] ses = {0};
//
//		SdfApi	face = new SdfApi();
//		try
//		{
//			ret = face.openDevice(dev);
//			if(ret != 0){
//				System.out.println("OpenDevice ret = " + ret);
//				return null;
//			}
//			ret = face.initialize(dev[0], sConfFilePath.getBytes());
//			if(ret != 0){
//				System.out.println("initialize ret=" + ret);
//				return null;
//			}
//			ret = face.openSession(dev[0], ses);
//			if(ret != 0){
//				System.out.println("openSession ret = " + ret);
//				return null;
//			}
//			ret = face.getPrivateKeyAccessRight(ses[0], iPrvKeyIndex, sPassword.getBytes(), sPassword.length());
//			if(ret != 0){
//				System.out.println("getPrivateKeyAccessRight ret = " + ret);
//				return null;
//			}
//			EccSignature es = new EccSignature();
//			ret = face.internalSignEcc(ses[0], iPrvKeyIndex,bHash, bHash.length, es);
//			if(ret != 0){
//				System.out.println("internalSignEcc ret = " + ret);
//				return null;
//			}
//			ret = face.releasePrivateKeyAccessRight(ses[0], iPrvKeyIndex);
//			if(ret != 0){
//				System.out.println("releasePrivateKeyAccessRight ret = " + ret);
//				return null;
//			}
//			//得到签名值
//			byte[] bSignVal = new byte[64];
//			byte[] bR = es.getR();
//			byte[] bS = es.getS();
//			int iOffset = 0;
//			if(bR.length > 32)
//				iOffset = 32;
//			System.arraycopy(bR, iOffset, bSignVal, 0, 32);
//			System.arraycopy(bS, iOffset, bSignVal, 32, 32);
//
//			//开始组装签名值
//			ASN1EncodableVector v = new ASN1EncodableVector();
//			ASN1EncodableVector tbs = CreateTbsEx(bSeal,sSignTime,bDocHash,sDocProperty,bCert);
//			//TBS
//			v.add(new DERSequence(tbs));
////			ASN1EncodableVector vSignVal = new ASN1EncodableVector();
////			byte[] b1 = new byte[32];
////			byte[] b2 = new byte[32];
////			System.arraycopy(bSignVal, 0, b1, 0, 32);
////			System.arraycopy(bSignVal, 32, b2, 0, 32);
////			BigInteger big1 = toBigInteger(b1);
////			BigInteger big2 = toBigInteger(b2);
////			vSignVal.add(new ASN1Integer(big1));
////			vSignVal.add(new ASN1Integer(big2));
////			DERSequence derSignVal = new DERSequence(vSignVal);
////			try {
////				bSignVal = derSignVal.getEncoded();
////			} catch (IOException e1) {
////				e1.printStackTrace();
////			}
//			//签名值
//			v.add(new DERBitString(bSignVal));
//			DERSequence der = new DERSequence(v);
//			try {
//				return der.getEncoded();
//			} catch (IOException e) {
//				e.printStackTrace();
//				return null;
//			}
//		}finally{
//			if(ses[0] != 0)
//				face.closeSession(ses[0]);
//			if(dev[0] != 0)
//				face.closeDevice(dev[0]);
//		}
		return null;
	}

	public static byte[] SignIt(byte[] bSeal,String sSignTime,byte[] bDocHash,String sDocProperty,byte[] bSignCert,String sAlgOid,String sPub,String sPrv) throws IOException
	{
		byte[] bForSign = CreateTbs(bSeal,sSignTime,bDocHash,sDocProperty,bSignCert);
		if(bForSign == null)
			return null;
		try {
			byte[] bSignVal = SM2CryptoForAgreement.SignDateForSeal(bSignCert, bForSign, sPub, sPrv);
			ASN1EncodableVector v = new ASN1EncodableVector();
			ASN1EncodableVector tbs = CreateTbsEx(bSeal,sSignTime,bDocHash,sDocProperty,bSignCert);
			//TBS
			v.add(new DERSequence(tbs));
//			ASN1EncodableVector vSignVal = new ASN1EncodableVector();
//			byte[] b1 = new byte[32];
//			byte[] b2 = new byte[32];
//			System.arraycopy(bSignVal, 0, b1, 0, 32);
//			System.arraycopy(bSignVal, 32, b2, 0, 32);
//			BigInteger big1 = toBigInteger(b1);
//			BigInteger big2 = toBigInteger(b2);
//			vSignVal.add(new ASN1Integer(big1));
//			vSignVal.add(new ASN1Integer(big2));
//			DERSequence derSignVal = new DERSequence(vSignVal);
//			bSignVal = derSignVal.getEncoded();
			//签名值
			v.add(new DERBitString(bSignVal));
			DERSequence der = new DERSequence(v);
			try {
				return der.getEncoded();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
